<?php
/**
 * 会员帐号管理
 * @author hushuai
 *
 */
class MemberAction extends CommonAction {
	
	private $Member = "Member" ;
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 新增会员账号
     * @access public
     * @return array
     */
    public function add(){
    	
    	$member = M($this->Member);
		
    	$map['email']    = $this->_req("username");
    	$map['username'] = $this->_req("username");
    	$map['mobile']   = $this->_req("mobile");
    	$map['isreg']    = $this->_req("isreg");
    	$map['password'] = myencrypt($this->_req("password"));
    	$map['status']   = $this->_req("status","trim","2");
    	$map['addtime']  = time();
    	$map['solevar']  = create_id();
    	
    	$where['email'] = $map['email'];
    	$where['isdel'] = 2;
    	$view = $member->where($where)->find();

    	if($view['id']&&$view['isreg']==1){
    		$this->error("Registration failed!");
    	}elseif($view['id']&&$view['isreg']==2){
    		$cat['id']       = $view['id'];
    		$cat['isreg']    = 1;
    		$cat['status']   = 1;
    		$cat['password'] = $map['password'];
    		$data = $member->save($cat);
    	}else{
    		$data = $member->add($map);
    	}
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("Registration failed！");
    	}
    }
    
    /**
     * 会员详情
     * @access public
     * @return array
     */
    public function detail(){
    	 
    	$member = M($this->Member);
    
    	$id   = $this->_req("id");
    	 
    	$this->success($member->find($id));
    }
    
    /**
     * 会员修改
     * @access public
     * @return array
     */
    public function edit(){
    	parent::do_edit($this->Member);
    }
    
    /**
     * 验证邮箱唯一性
     * @access public
     * @param  String email
     * @return array
     */
    public function checked(){
    	
    	$map['username'] = $this->_req("param");
    	$map['isdel']  = 2;
    	
    	$data = M($this->Member)->where($map)->find(); 
    	
   		if($data['id']){
   			$this->error(L('vip_tips_vip_yicunz'));
		}else{
			$this->success("Prompt information");
		}
    }
    
    /**
     * 检验密码
     * @access public
     * @param  String Password
     * @return array
     */
    public function checkPassword(){
    	 
    	$map['password'] = myencrypt($this->_req("password"));
    	//$map['password'] = $this->_req("password");
    	$map['id']       = $this->_req("id");
    	 
    	$data = M($this->Member)->where($map)->find();
    	//echo M($this->Member)->getLastSql();
    	 
    	if($data){
    		$this->success("Prompt information");
    	}else{
    		$this->error("Old password input error！"); //旧密码输入错误
    	}
    }
    
    /**
     * 检验密码
     * @access public
     * @param  String mobile
     * @return array
     */
    public function editPassword(){
    
    	$map['password'] = myencrypt($this->_req("password"));
    	$map['id']       = $this->_req("id");
    
    	$data = M($this->Member)->save($map);
    
    	if($data){
    		$this->success("Eidt Success");
    	}else{
    		$this->error("Eidt Faile！"); //修改失败
    	}
    }

    /**
     * 登陆
     * @access public
     * @param  String mobile
     * @param  String password
     * @return array
     */
    public function login(){

    	$map['isdel']    = 2;
    	$map['isreg']    = 1;
    	$map['email']    = $this->_req("email");
    	$password = myencrypt($this->_req("password"));
    	$data = M($this->Member)->where($map)->find();
    	
    	if($data['id'] && $data['password']==""){
    		
    		$rows['id'] = $data['id'];
    		$rows['password'] = $password;
    		$data = M($this->Member)->save($rows);
    		$data = M($this->Member)->find($data);
    		
    		$this->success($data);
    	}else{
    		
    		$map['isdel']    = 2;
    		$map['isreg']    = 1;
    		$map['email']    = $this->_req("email");
    		$map['password'] = $password;
    		$data = M($this->Member)->where($map)->find();
    		
	    	if($data['id']){
	    		if($data['isreg']==1){
	    			$this->success($data);
	    		}else{
	    			$this->error(L('vip_tips_login_account'));//您的账号没有被激活
	    		}
	    	}else{
	    		$this->error(L('vip_tips_login_exsits')); //账号或密码输入错误
	    	}
    	}
    }
    
    /**
     * 删除会员账号
     * @access public
     * @param  Int   userid 会员账号id
     * @return array
     */
    public function del(){
    
    	$userid = $this->_req("userid");
    
    	$data = D("Member")->del($userid);
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("会员账号删除失败，请稍后重试");
    	}
    }
    
    /**
     * 冻结会员账号(将激活状态改成-未激活)
     * @access public
     * @param  Int   userid 会员账号id
     * @return array
     */
    public function frost(){
    
    	$userid = $this->_req("userid");
    	
    	$data = D("Member")->frost($userid);
    	 
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("会员账号冻结失败，请稍后重试");
    	}
    
    }
    
    /**
     * 解除冻结(激活)
     * @access public
     * @param  Int   userid 会员账号id
     * @return array
     */
    public function active(){
    
    	$userid = $this->_req("userid");
    	 
    	$data = D("Member")->frost($userid);
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("会员账号解除冻结失败，请稍后重试");
    	}
    	
    }
    
}